package com.example.android.miwok;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

public class FamilyActivity extends AppCompatActivity {
    private MediaPlayer mMediaPlayer;
    private AudioManager mAudioManager;
    private AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int focusChange) {
            if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
                    focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
                mMediaPlayer.pause();
                mMediaPlayer.seekTo(0);
            } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                mMediaPlayer.start();
            } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
                releaseMediaPlayer();
            }
        }
    };
    // 检测播放是否结束
    private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            releaseMediaPlayer();
            //Toast.makeText(NumbersActivity.this, "IM Done", Toast.LENGTH_SHORT).show();
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.word_list);

        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        // Initial words list in ArrayList
        final ArrayList<Word> words = new ArrayList<Word>() {{
            String[] miWorkWords = {"你爸", "尼玛", "你儿子", "你女儿", "你老哥", "你小弟", "你小妹",
                    "你老妹", "尼玛的妈", "你爹的爹"};
            String[] defaultWords = {"father", "mother", "son", "daughter", "older brother",
                    "younger brother", "older sister ", "younger sister", "grandmother", "grandfather"};
            int[] memberImages = {R.drawable.family_father, R.drawable.family_mother, R.drawable.family_son, R.drawable.family_daughter,
                    R.drawable.family_older_brother, R.drawable.family_younger_brother, R.drawable.family_younger_sister,
                    R.drawable.family_older_sister, R.drawable.family_grandmother, R.drawable.family_grandfather};
            int[] numberAudios = {R.raw.family_father, R.raw.family_mother, R.raw.family_son, R.raw.family_daughter,
                    R.raw.family_older_brother, R.raw.family_younger_brother, R.raw.family_younger_sister,
                    R.raw.family_older_sister, R.raw.family_grandmother, R.raw.family_grandfather};

            int index = 0;
            while (index < miWorkWords.length) {
                Word w = new Word(miWorkWords[index], defaultWords[index], memberImages[index], numberAudios[index]);
                add(w);
                index++;
            }
        }};

        // Create a ArrayAdapter
        WordAdapter itemAdapter = new WordAdapter(this, words, R.color.category_family);

        ListView listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(itemAdapter);

        //  add listView onClickListener method
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Release the media player if it currently exists because we are about to
                // play a different sound file
                releaseMediaPlayer();

                // Search the certain position in your words list
                Word word = words.get(position);

                int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener,
                        AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
                if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                    mMediaPlayer = MediaPlayer.create(FamilyActivity.this, word.getAudioResourceId());
                    mMediaPlayer.start();
                    mMediaPlayer.setOnCompletionListener(mCompletionListener);
                }
            }
        });


    }

    @Override
    protected void onStop() {
        super.onStop();
        releaseMediaPlayer();
    }

    /**
     * Clean up the media player by releasing its resources.
     */
    private void releaseMediaPlayer() {
        // If the media player is not null, then it may be currently playing a sound.
        if (mMediaPlayer != null) {
            // Regardless of the current state of the media player, release its resources
            // because we no longer need it.
            mMediaPlayer.release();

            // Set the media player back to null. For our code, we've decided that
            // setting the media player to null is an easy way to tell that the media player
            // is not configured to play an audio file at the moment.
            mMediaPlayer = null;

            // When finished playing, we don't need focus anymore.
            mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener);
        }
    }

}
